package com.rfid.a252robot.admin;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.google.gson.reflect.TypeToken;
import com.rfid.a252robot.R;
import com.rfid.a252robot.admin.adapter.AddressAdapter;
import com.rfid.a252robot.admin.bean.Address;
import com.rfid.a252robot.base.Listener;
import com.rfid.a252robot.event.MapInfoEvent;
import com.rfid.common.log.LogUtils;
import com.rfid.mqtt.MqttManager;
import com.rfid.mqtt.bean.MapInfo;
import com.rfid.util.ACache;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;


public class RouteSelectActivity extends Activity{
    @BindView(R.id.recyclerView1)
    RecyclerView mSelectRecyclerView;
    @BindView(R.id.recyclerView2)
    RecyclerView mLeftRecyclerView;
    AddressAdapter mSelectAdapter,mLeftAdapter;
    List<Address> mSelect,mLeft;
    ItemTouchHelper mItemTouchHelper;

    Listener mSelectListener = new Listener<Address>() {
        @Override
        public void onClick(Address o) {
            mSelect.remove(o);
            mLeft.add(o);
            mLeftAdapter.notifyDataSetChanged();
            mSelectAdapter.notifyDataSetChanged();
        }
    };

    Listener mLeftListener = new Listener<Address>() {
        @Override
        public void onClick(Address o) {
            mLeft.remove(o);
            mSelect.add(o);
            mLeftAdapter.notifyDataSetChanged();
            mSelectAdapter.notifyDataSetChanged();
        }
    };

    ItemTouchHelper.Callback mCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT,0) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            System.out.println("onMove " + viewHolder.getAdapterPosition() + ":" + target.getAdapterPosition());
            mSelect.add(target.getAdapterPosition(),mSelect.remove(viewHolder.getAdapterPosition()));
            mSelectAdapter.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
            return true;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
//            mSelectAdapter.notifyDataSetChanged();
            System.out.println("onSwiped direction = " + direction);
        }
    };

    RecyclerView.ItemDecoration mItemDecoration = new RecyclerView.ItemDecoration() {

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            outRect.set(10, 10, 10, 10);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exhibition);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mItemTouchHelper = new ItemTouchHelper(mCallback);
        mSelect = new ArrayList<>();
        mLeft = new ArrayList<>();
        mSelectAdapter = new AddressAdapter(mSelect);
        mSelectAdapter.setListener(mSelectListener);
        mLeftAdapter = new AddressAdapter(mLeft);
        mLeftAdapter.setListener(mLeftListener);
        mSelectRecyclerView.setLayoutManager(new GridLayoutManager(this,5));
        mSelectRecyclerView.setAdapter(mSelectAdapter);
        mSelectRecyclerView.addItemDecoration(mItemDecoration);
        mLeftRecyclerView.setAdapter(mLeftAdapter);
        mLeftRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(5,StaggeredGridLayoutManager.VERTICAL));
        mLeftRecyclerView.addItemDecoration(mItemDecoration);
        mItemTouchHelper.attachToRecyclerView(mSelectRecyclerView);
//        List<Address> addresses = ACache.get().getAsAddresses("routes");
        MqttManager.INSTANCE.getMapInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMapInfo(MapInfoEvent event) {
        LogUtils.v("onMapInfo = " + event.getInfo());
        mSelect.clear();
        mLeft.clear();
        MapInfo info = event.getInfo();
        List<MapInfo.DataBeanX.DataBean> locations = info.getData().getData();
        List<Address> addresses = new ArrayList<>();
        for (MapInfo.DataBeanX.DataBean bean : locations) {
            if (bean.getActive() != 1) {
                continue;
            }
            List<MapInfo.DataBeanX.DataBean.BizPointsBean>  points = bean.getBizPoints();
            for (MapInfo.DataBeanX.DataBean.BizPointsBean point : points) {
                Address address = new Address();
                address.setBizPointId(point.getBizPointId());
                address.setPointName(point.getPointName());
                addresses.add(address);
            }
        }
        List<Address> cacheAddresses = ACache.get().getAsAddresses("routes");
        ACache.get().put("routes",addresses);
        if (cacheAddresses.size() == addresses.size()) {
            cacheAddresses.removeAll(addresses);
            if (cacheAddresses.size() == 0 ) {
                List<Address> ls =  ACache.get().getAsAddresses("leftRoutes");
                List<Address> ss = ACache.get().getAsAddresses("selectRoutes");
                if (ls.size() + ss.size() != addresses.size()) {
                    mLeft.clear();
                    mSelect.clear();
                    mLeft.addAll(addresses);
                } else {
                    if (ls.size() > 0 || ss.size() > 0) {
                        mLeft.addAll(ls);
                        mSelect.addAll(ss);
                    } else {
                        mLeft.addAll(addresses);
                    }
                }
            } else {
                mLeft.clear();
                mSelect.clear();
                mLeft.addAll(addresses);
            }
        } else {
            mLeft.clear();
            mSelect.clear();
            mLeft.addAll(addresses);
        }
        mLeftAdapter.notifyDataSetChanged();
        mSelectAdapter.notifyDataSetChanged();
    }

    public void back(View v) {
        finish();
    }

    public void confirm(View v) {
        LogUtils.v(mSelect);
        ACache.get().put("selectRoutes",mSelect);
        ACache.get().put("leftRoutes",mLeft);
        ACache.get().put("current","");
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
